/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.my.bigsite.modules.act.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.ExclusiveGateway;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.FlowNode;
import org.activiti.bpmn.model.GraphicInfo;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.UserTask;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.impl.cmd.GetBpmnModelCmd;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import com.my.bigsite.common.activiti.support.EdgeDTO;
import com.my.bigsite.common.activiti.support.NodeDTO;
import com.my.bigsite.common.util.StringUtils;
import com.my.bigsite.common.web.BaseController;
import com.my.bigsite.modules.act.service.Conf_User_Service;
import com.my.bigsite.modules.act.util.BpmnUtil;

/**
 * 流程定义相关Controller
 * 
 * @author wwy
 * @version 2013-11-03
 */
@Controller
@RequestMapping(value = "${adminPath}/act/confNode")
public class ConfNodeController extends BaseController {

	@Autowired
	ProcessEngine processEngine;
	@Autowired
	private RepositoryService repositoryService;
	@Autowired
	RuntimeService runtimeService;
	@Autowired
	TaskService taskService;
	@Autowired
	Conf_User_Service conf_User_Service;

	/**
	 * 流程定义列表
	 */
	@RequestMapping(value = "list/{procInsId}")
	public ModelAndView processList(
			@PathVariable("procInsId") String procInsId,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		//获取流程定义的所有节点元素
		List<NodeDTO> nodeDtos = BpmnUtil.getNodeByProcess(procInsId,
				processEngine);
		ModelAndView view = new ModelAndView();
		view.addObject("NodeList", nodeDtos);
		view.addObject("pid", procInsId);
		view.setViewName("modules/act/confNodelist");
		return view;
	}

	/**
	 * 保存设置用户
	 */
	@RequestMapping(value = "saveUser")
	public String saveUser(String type, String taskAssigneeNames,
			String procinsid, String nodeid) {
		Map<String, Object> mp = new HashMap<String, Object>();
		if (type.equals("负责人")) {
			mp.put("TYPE", 0);
		} else if (type.equals("候选人")) {
			mp.put("TYPE", 1);
		} else {
			mp.put("TYPE", 2);
		}
		mp.put("TYPE", type);
		mp.put("ID", System.currentTimeMillis());
		mp.put("VALUE", taskAssigneeNames);
		mp.put("PROCINSID", procinsid);
		mp.put("NODEID", nodeid);
		conf_User_Service.saveUser(mp);
		return "redirect:" + adminPath + "/act/confNode/list/" + procinsid;
	}

}
